﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace mlnx_os_sysdump_analyzer
{
    class Dmesg
    {
        //private string erros = "";
        private string full = "";
        private bool KEEP_RUNNING; 
        private string OUTPUT_DIR_PATH;
        private System.Collections.Specialized.StringCollection dmesgKeywords = Properties.Settings.Default.dmesgKeywords;
        //
        //----------------------------------------------------
        //
        public Dmesg(ref bool KEEP_RUNNING, ref string OUTPUT_DIR_PATH)
        {
            this.KEEP_RUNNING = KEEP_RUNNING;   
            this.OUTPUT_DIR_PATH = OUTPUT_DIR_PATH;
        }
        //
        //----------------------------------------------------
        //
        public void scanInfo()
        {
            string path = Methods.FindFileRec(OUTPUT_DIR_PATH, "dmesg");
            if (path != null)
            {
                scanForErrors(path);
            }
        }
        //
        //----------------------------------------------------
        //
        private void scanForErrors(object param)
        {
            string srcFilePath = (string)param;
            if (srcFilePath == null || !File.Exists(srcFilePath))
                return;
            StreamReader sr = new StreamReader(srcFilePath);
            try
            {
                while (KEEP_RUNNING && sr.Peek() >= 0)
                {
                    // read line
                    string line = sr.ReadLine();
                    try
                    {
                        // add to full
                        full += line + "\r\n";
                        // check for errors
                        //for (int i = 0; i < dmesgKeywords.Count; i++)
                        //{
                        //    if (line.Contains(dmesgKeywords[i]))
                        //    {
                        //        erros += line + "\r\n";
                        //        break;
                        //    }
                        //}
                    }
                    catch
                    {
                    }
                }
            }
            catch { }
            sr.Dispose();
        }
        //
        //----------------------------------------------------
        //
        public string getErrors()
        {
            return full;
            //if (!erros.Equals(""))
            //{
            //    return new string[] { erros, full };
            //}
            //else
            //{
            //    return new string[] { "******  No errors were found in this file.  ******", full };
            //}
        }
        //
        //----------------------------------------------------
        //
        //public string checkForPLBerr()
        //{
        //    if (erros.Contains("PLB"))
        //    {
        //        return "* ERROR: There are PLB errors on the switch.\r\n"
        //            + "More details can be found under the 'Errors' -> 'From dmesg' tab."
        //            + "\r\n\r\n";
        //    }
        //    else
        //    {
        //        return "";
        //    }
        //}
        //
        //----------------------------------------------------
        //
    }
}
